package sis.search;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-02 18:01
 * @notify
 * @version 1.0
 */

import java.util.Timer;
import java.util.TimerTask;

public class SearchScheduler {
    private ResultsListener listener;
    private Timer timer;

    public SearchScheduler(ResultsListener listener) {
        this.listener = listener;
    }

    public void repeat(final Search search, long interval) {
        timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                search.execute();
                listener.executed(search);
            }
        };
        //开始 执行 task任务, 0秒延迟 ,interval执行一次
        timer.scheduleAtFixedRate(task,0,interval);
    }

    //终止此计时器，丢弃任何当前计划的任务。
    public void stop() {
        timer.cancel();
    }
}
